//工具方法
import dayjs from "dayjs";
import weekOfYear from "dayjs/plugin/weekOfYear";
import isoWeek from "dayjs/plugin/isoWeek";
dayjs.extend(weekOfYear);
dayjs.extend(isoWeek);


//模拟sleep
async function sleep(seconds = 0){
    return new Promise((resolve,reject) => {
        setTimeout(() => {
            resolve('end');
        }, seconds);
    })
}


/**
 * 通过精致token获取登录用户信息
 * @param token 
 * @returns 用户信息 | 错误提示
 */
const getUserInfoByToken = (token = null) => {
    const isDevelopment = process.env.NODE_ENV === 'development';
    if(token === null) {
        if(isDevelopment){
            token = process.env.VUE_APP_APPLICATION_TOKEN;
        } else {
            token = localStorage.getItem("Token");
        }
    }
    const strings = token.split('.') //通过split()方法将token转为字符串数组
    //取strings[1]数组中的第二个字符进行解析
    const userinfo = JSON.parse(decodeURIComponent(escape(window.atob(strings[1].replace(/-/g, '+').replace(/_/g, '/')))));
    return userinfo;
}

//产生随机数
const getRandomId = () => {
    const timeStamp = new Date().getTime();
    let random = Math.random() * 1000;
    random = random < 100 ? random + 100 : random;
    return parseInt(timeStamp + '' + random);
}

const T = {
    sleep,
    getUserInfoByToken,
    getRandomId,
    dayjs
}

export default T;